<?php

/**
 * @author 		Christopher Hazekamp
 * @copyright 	2011
 */

if (!defined('PUBLIC_HTML'))
{
	exit;
}

$submit = (isset($_POST['submit'])) ? true : false;

/***********************************
* POST ACTIONS
***********************************/
if ($submit)
{
	$required_fields = array(
		'name'
	);
	
	if (!$validator->check_required($required_fields))
	{
		redirect('index.php?p=media');
	}
	else
	{
		$upload_file = $file->upload('mediafile', 'media/', 'jpg,jpeg,jpe,png,gif');
		$file_extensions = preg_match_all('/./', $upload_file);
		$file_extension = preg_split('/./', $upload_file);
		
		$post = array();
		$post['name']		= $db->sql_injection($_POST['med_title']);
		$post['type']		= MEDIA_IMAGE;
		$post['extension']	= $file_extension[$file_extensions];
		$post['rel_id']		= (int) $_POST['rel_id'];
		$post['rel_type'] 	= 1;
		$db->sql_insert_row(TABLE_MEDIA, $post, 'med_');
	}
}

/***********************************
* MEDIA OVERVIEW
***********************************/
if ($db->sql_count(TABLE_MEDIA) >= 1)
{
	$get_media =	"
					SELECT		*
					FROM		" . TABLE_MEDIA . "
					ORDER BY	med_id
					DESC
					";
	$sql_media =	$db->sql_query($get_media);
	while ($row_media = $db->sql_fetch_assoc($sql_media))
	{
		$tpl_content->newBlock("GET MEDIA");
		
		// Get related content
		switch ($row_media['med_rel_type'])
		{
			case MEDIA_REL_PAGE:
				$get_rel_media =	"
									SELECT		*
									FROM		" . TABLE_PAGES . "
									WHERE		page_id = '" . $row_media['med_rel_id'] . "
									LIMIT		1
									";
			break;
			
			case MEDIA_REL_DUMP:
				$get_rel_media = '';
			break;
		}
		
		if ($get_rel_media != '')
		{
			$sql_rel_media = $db->sql_query($get_rel_media);
			$row_rel_media = $db->sql_fetch_assoc($sql_rel_media);
			
		}
	}
}

?>